package com.hmkj.core.service.operate.impl;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.constant.RiskNID;
import com.hmkj.core.mapper.commo.CommoMapper;
import com.hmkj.core.mapper.operate.AgentConfigMapper;
import com.hmkj.core.mapper.operate.AgentMapper;
import com.hmkj.core.model.operate.AgentModel;
import com.hmkj.core.po.commo.Commo;
import com.hmkj.core.po.operate.Agent;
import com.hmkj.core.po.operate.AgentConfig;
import com.hmkj.core.service.operate.AgentService;
import com.hmkj.core.service.risk.RiskParamService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商表业务相关Service接口实现<br>
 *
 * @ClassName: AgentServiceImpl
 * @author bin
 * @date   2017-11-07 02:41:04
 */
@Service
@Scope("prototype")
@Transactional(rollbackFor = Exception.class)
public class AgentServiceImpl implements AgentService {

	@Resource
    private AgentMapper agentMapper;
    @Resource
    private AgentConfigMapper agentConfigMapper;
    @Resource
    private RiskParamService riskParamService;
    @Resource
    private CommoMapper commoMapper;

    @Override
    public GenericMapper<Agent,Integer> _getMapper() {
        return agentMapper;
    }

    @Override
    public void addAgent(AgentModel agentModel) throws Exception {
        Agent agent = new Agent();
        BeanUtils.copyProperties(agentModel,agent);
        agent.setCreateTime(new Date());
        agentMapper.insert(agent);
        List<Commo> commos = commoMapper.select(new Commo(c->{
            c.setDelFlag(Commo.DELFLAG.NORMAL.code);
        }));
        Map<String,String> map = new HashMap<>();
        for(Commo commo :commos){
            String fee = riskParamService.getParamValue(RiskNID.FEE,commo.getId());
            map.put(commo.getCode(),fee);
        }
        List<Agent> agents = agentMapper.select(new Agent(a ->{
            a.setName(agentModel.getName());
        }));
        if (agents!=null&&agents.size()>0){
            Agent agent1 = agents.get(0);
            AgentConfig agentConfig =  new AgentConfig();
            agentConfig.setNid("gc_fee");
            agentConfig.setName("美黄金手续费");
            agentConfig.setRemark("美黄金手续费");
            agentConfig.setAgentId(agent1.getId());
            agentConfig.setCreateTime(new Date());
            agentConfig.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            if(agentModel.getGcFee()!=null){
                agentConfig.setValue(agentModel.getGcFee().toString());
                agentConfigMapper.insert(agentConfig);
            }else{
                if (map.containsKey("GC")){
                    agentConfig.setValue(map.get("GC"));
                }
                agentConfigMapper.insert(agentConfig);
            }
            agentConfig =  new AgentConfig();
            agentConfig.setNid("cl_fee");
            agentConfig.setName("美原油手续费");
            agentConfig.setRemark("美原油手续费");
            agentConfig.setAgentId(agent1.getId());
            agentConfig.setCreateTime(new Date());
            agentConfig.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            if(agentModel.getClFee()!=null){
                agentConfig.setValue(agentModel.getClFee().toString());
                agentConfigMapper.insert(agentConfig);
            }else{
                if (map.containsKey("CL")){
                    agentConfig.setValue(map.get("CL"));
                }
                agentConfigMapper.insert(agentConfig);
            }
            agentConfig =  new AgentConfig();
            agentConfig.setNid("si_fee");
            agentConfig.setName("美白银手续费");
            agentConfig.setRemark("美白银手续费");
            agentConfig.setAgentId(agent1.getId());
            agentConfig.setCreateTime(new Date());
            agentConfig.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            if(agentModel.getSiFee()!=null){
                agentConfig.setValue(agentModel.getSiFee().toString());
                agentConfigMapper.insert(agentConfig);
            }else{
                if (map.containsKey("SI")){
                    agentConfig.setValue(map.get("SI"));
                }
                agentConfigMapper.insert(agentConfig);
            }
            agentConfig =  new AgentConfig();
            agentConfig.setNid("hsi_fee");
            agentConfig.setName("恒指手续费");
            agentConfig.setRemark("恒指手续费");
            agentConfig.setAgentId(agent1.getId());
            agentConfig.setCreateTime(new Date());
            agentConfig.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            if(agentModel.getHsiFee()!=null){
                agentConfig.setValue(agentModel.getHsiFee().toString());
                agentConfigMapper.insert(agentConfig);
            }else {
                if (map.containsKey("HSI")){
                    agentConfig.setValue(map.get("HSI"));
                }
                agentConfigMapper.insert(agentConfig);
            }
            agentConfig =  new AgentConfig();
            agentConfig.setNid("mhi_fee");
            agentConfig.setName("小恒指手续费");
            agentConfig.setRemark("小恒指手续费");
            agentConfig.setAgentId(agent1.getId());
            agentConfig.setCreateTime(new Date());
            agentConfig.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            if(agentModel.getMhiFee()!=null){
                agentConfig.setValue(agentModel.getMhiFee().toString());
                agentConfigMapper.insert(agentConfig);
            }else {
                if (map.containsKey("MHI")){
                    agentConfig.setValue(map.get("MHI"));
                }
                agentConfigMapper.insert(agentConfig);
            }
            agentConfig =  new AgentConfig();
            agentConfig.setNid("dax_fee");
            agentConfig.setName("德指手续费");
            agentConfig.setRemark("德指手续费");
            agentConfig.setAgentId(agent1.getId());
            agentConfig.setCreateTime(new Date());
            agentConfig.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            if(agentModel.getDaxFee()!=null){
                agentConfig.setValue(agentModel.getDaxFee().toString());
                agentConfigMapper.insert(agentConfig);
            }else{
                if (map.containsKey("DAX")){
                    agentConfig.setValue(map.get("DAX"));
                }
                agentConfigMapper.insert(agentConfig);
            }
        }
    }


    public void  editAgent(AgentModel agentModel) throws  Exception{
        Agent agent = new Agent();
        BeanUtils.copyProperties(agentModel,agent);
        agent.setCreateTime(new Date());
        agentMapper.updateByPrimaryKeySelective(agent);
        List<Commo> commos = commoMapper.select(new Commo(c->{
            c.setDelFlag(Commo.DELFLAG.NORMAL.code);
        }));
        Map<String,String> map = new HashMap<>();
        for(Commo commo :commos){
            String fee = riskParamService.getParamValue(RiskNID.FEE,commo.getId());
            map.put(commo.getCode(),fee);
        }
        AgentConfig acgc = agentConfigMapper.selectOne(new AgentConfig(aco->{
            aco.setAgentId(agentModel.getId());
            aco.setNid("gc_fee");
            aco.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
        }));
        if(agentModel.getGcFee()!=null){
            acgc.setValue(agentModel.getGcFee().toString());
        }else{
            if(map.containsKey("GC")){
                acgc.setValue(map.get("GC"));
            }
        }
        agentConfigMapper.updateByPrimaryKeySelective(acgc);
        AgentConfig accl = agentConfigMapper.selectOne(new AgentConfig(aco->{
            aco.setAgentId(agentModel.getId());
            aco.setNid("cl_fee");
            aco.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
        }));
        if(agentModel.getClFee()!=null){
            accl.setValue(agentModel.getClFee().toString());
        }else{
            if(map.containsKey("CL")){
                accl.setValue(map.get("CL"));
            }
        }
        agentConfigMapper.updateByPrimaryKeySelective(accl);
        AgentConfig acsi = agentConfigMapper.selectOne(new AgentConfig(aco->{
            aco.setAgentId(agentModel.getId());
            aco.setNid("si_fee");
            aco.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
        }));

        if(agentModel.getSiFee()!=null){
            acsi.setValue(agentModel.getSiFee().toString());
        }else{
            if(map.containsKey("SI")){
                acsi.setValue(map.get("SI"));
            }
        }
        agentConfigMapper.updateByPrimaryKeySelective(acsi);
        AgentConfig achsi = agentConfigMapper.selectOne(new AgentConfig(aco->{
            aco.setAgentId(agentModel.getId());
            aco.setNid("hsi_fee");
            aco.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
        }));
        if(agentModel.getHsiFee()!=null){
            achsi.setValue(agentModel.getHsiFee().toString());
        }else{
            if(map.containsKey("HSI")){
                achsi.setValue(map.get("HSI"));
            }
        }
        agentConfigMapper.updateByPrimaryKeySelective(achsi);
        AgentConfig acmhi = agentConfigMapper.selectOne(new AgentConfig(aco->{
            aco.setAgentId(agentModel.getId());
            aco.setNid("mhi_fee");
            aco.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
        }));
        if(agentModel.getMhiFee()!=null){
            acmhi.setValue(agentModel.getMhiFee().toString());
        }else{
            if(map.containsKey("MHI")){
                acmhi.setValue(map.get("MHI"));
            }
        }
        agentConfigMapper.updateByPrimaryKeySelective(acmhi);
        AgentConfig acdax = agentConfigMapper.selectOne(new AgentConfig(aco->{
            aco.setAgentId(agentModel.getId());
            aco.setNid("dax_fee");
            aco.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
        }));
        if(agentModel.getDaxFee()!=null){
            acdax.setValue(agentModel.getDaxFee().toString());
        }else{
            if(map.containsKey("DAX")){
                acdax.setValue(map.get("DAX"));
            }
        }
        agentConfigMapper.updateByPrimaryKeySelective(acdax);
    }

    @Override
    public void deleteAgent(Integer id) throws Exception {
        Agent agent = new Agent();
        agent.setId(id);
        agent.setDelFlag(Agent.DELFLAG.DELETED.code);
        agentMapper.updateByPrimaryKeySelective(agent);
        List<AgentConfig> agentConfigs = agentConfigMapper.select(new AgentConfig(ac->{
            ac.setDelFlag(AgentConfig.DELFLAG.NORMAL.code);
            ac.setAgentId(id);
        }));
        if(agentConfigs!=null&&agentConfigs.size()>0){
            agentConfigs.forEach(agentConfig -> {
                agentConfig.setDelFlag(AgentConfig.DELFLAG.DELETED.code);
                agentConfigMapper.updateByPrimaryKeySelective(agentConfig);
            });
        }
    }

    @Override
    public Agent selectAgentByDomain(String domain) {
        return agentMapper.selectAgentByDomain(Agent.STATUS.T1.code,domain);
    }
}
